var gulp = require('gulp');
var glob = require('glob');

var rjs = require('gulp-requirejs');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var less = require('gulp-less');
var clean = require('gulp-clean');
var zip = require("gulp-zip");
var watch = require("gulp-watch");

var replace = require("gulp-replace");
var jshint = require("gulp-jshint");
var connect = require('gulp-connect'); 

gulp.task("css", function () {
    gulp.src('./assets/theme.*.less')
        .pipe(sourcemaps.init())
        .pipe(less({compress: true}))
        .pipe(sourcemaps.write("./"))
        .pipe(gulp.dest('./deploy/assets')).pipe(connect.reload());
});

gulp.task('js', function () {
    var views = glob.sync("views/**/*.js").map(function (value) {
        return value.replace(".js", "");
    });

    var locales = glob.sync("locales/**/*.js").map(function (value) {
        return value.replace(".js", "");
    });

    rjs({
        baseUrl: './',
        out: 'app.js',
        insertRequire: ["app"],
        paths: {
            "locale": "empty:",
            "text": 'libs/text'
        },
        deps: ["app"],
        include: ["libs/almond.js"].concat(views).concat(locales)
    })
        .pipe(sourcemaps.init())
        // .pipe(uglify())
        .pipe(sourcemaps.write("./"))
        .pipe(gulp.dest('./deploy/')).pipe(connect.reload());
});

gulp.task("clean", function () {
    return gulp.src("deploy/*", {read: false}).pipe(clean());
});

gulp.task('build', ["js", "css"], function () {
    var build = (new Date()) * 1;

    return require('event-stream').merge(
        //assets
        gulp.src("./assets/imgs/**/*.*")
            .pipe(gulp.dest("./deploy/assets/imgs/")),
        //libs
        // gulp.src("./libs/webix/**/*.*")
        //     .pipe(gulp.src("./libs/editor/**/*.*"))
        //     .pipe(gulp.dest("./deploy/libs/webix/"))
        //     .pipe(gulp.dest("./deploy/libs/editor/")),
        gulp.src("./libs/**/*.*")
            .pipe(gulp.dest("./deploy/libs/"))
            .pipe(connect.reload()),
        //index
        gulp.src("./index.html")
            .pipe(replace('data-main="app" src="libs/require.js"', 'src="app.js"'))
            .pipe(replace('<script type="text/javascript" src="libs/less.min.js"></script>', ''))
            .pipe(replace(/rel\=\"stylesheet\/less\" href=\"(.*?)\.less\"/g, 'rel="stylesheet" href="$1.css"'))
            .pipe(replace(/\.css\"/g, '.css?' + build + '"'))
            .pipe(replace(/\.js\"/g, '.js?' + build + '"'))
            .pipe(replace("require.config", "webix.runningEnv = '" + process.env.NODE_ENV + "'; require.config"))
            // .pipe(replace(/\.\.\/webix\/codebase\//g, '//cdn.webix.com/site/'))
            // .pipe(replace(/cdn\.webix\.com\/edge/g, 'cdn.webix.com/site'))

            .pipe(gulp.dest("./deploy/"))
            .pipe(connect.reload())

        //server
        // gulp.src("./server/**/*.*")
        //     .pipe(gulp.dest("./deploy/server/"))
    );

});

gulp.task("sources", function () {
    gulp.src(["assets/**/*", "helpers/**/*", "libs/**/*", "models/**/*", "util/*", "server/**/*", "views/**/*", "index.html", "app.js", "package.json"],
        {base: './'})
    // .pipe(zip("webix-admin-app.sources.zip"))
        .pipe(gulp.dest('./'));
});

gulp.task('lint', function () {
    return gulp.src('./views/**/*.js', './helpers/**/*.js', './models/**/*.js', './configs/**/*.js', './*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'))
        .pipe(jshint.reporter('fail'));
});

gulp.task('watch', function () {
    gulp.watch('*.js', ['build']);
    gulp.watch('assets/*.less', ['build']);
    gulp.watch('models/*.js', ['build']);
    gulp.watch('util/*.js', ['build']);
    gulp.watch('helpers/*.js', ['build']);
    gulp.watch('views/**/*.js', ['build']);
});

gulp.task('connect', function () {
    connect.server({
        port: '8090',
        livereload: true
    });
});

gulp.task('set-dev', [], function () {
    return process.env.NODE_ENV = 'development';
});
gulp.task('set-prod', [], function () {
    return process.env.NODE_ENV = 'production';
});
gulp.task('set-build-ali-test', [], function () {
    return process.env.NODE_ENV = 'ali-test';
});
gulp.task('set-build-local-test', [], function () {
    return process.env.NODE_ENV = 'local-test';
});

gulp.task('serve', ['set-dev', 'build', 'watch', 'connect'], function () {

});

gulp.task('build-ali-test', ['set-build-ali-test','build'], function () {

});

gulp.task('build-local-test', ['set-build-local-test','build'], function () {

});

gulp.task('build-prod', ['set-prod', 'build'], function () {
});



